 package scripting.quest;

 import client.MapleClient;
 import configs.ServerConfig;
 import constants.ScriptType;
 import constants.ServerConstants;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.WeakHashMap;
 import javax.script.Invocable;
 import javax.script.ScriptEngine;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import scripting.AbstractScriptManager;
 import server.quest.MapleQuest;
 import tools.MaplePacketCreator;








 public class QuestScriptManager
   extends AbstractScriptManager
 {
/*  29 */   private static final Logger log = LogManager.getLogger(QuestScriptManager.class.getName());
/*  30 */   private static final QuestScriptManager instance = new QuestScriptManager();
/*  31 */   private final Map<MapleClient, QuestActionManager> qms = new WeakHashMap<>();

/*  33 */   private final List<Integer> startQuestIds = new LinkedList<>();
/*  34 */   private final List<Integer> endQuestIds = new LinkedList<>();

   public static synchronized QuestScriptManager getInstance() {
/*  37 */     return instance;
   }

   public void startQuest(MapleClient c, int npcId, int questId) {
/*  41 */     if (c.getPlayer().isInJailMap()) {
/*  42 */       c.getPlayer().dropMessage(1, "在这个地方无法进行任务操作.");
       return;
     }
/*  45 */     if (!MapleQuest.getInstance(questId).canStart(c.getPlayer(), Integer.valueOf(npcId))) {
/*  46 */       if (c.getPlayer().isAdmin()) {
/*  47 */         c.getPlayer().dropMessage(6, "startQuest - 不能开始这个任务 NPC：" + npcId + " Quest：" + questId);
/*  48 */         c.announce(MaplePacketCreator.enableActions());
       }
       return;
     }
     try {
/*  53 */       if (this.qms.containsKey(c)) {
/*  54 */         dispose(c);
         return;
       }
/*  57 */       Invocable iv = getInvocable("quest/" + questId + ".js", c, true);
/*  58 */       if (iv == null) {
/*  59 */         if (c.getPlayer().isAdmin() && ServerConfig.WORLD_SHOWQUESTINFO) {
/*  60 */           c.getPlayer().dropMessage(5, "开始任务脚本不存在 NPC：" + npcId + " Quest：" + questId);
         }
/*  62 */         if (!this.startQuestIds.contains(Integer.valueOf(questId)));


/*  65 */         dispose(c);
         return;
       }
/*  68 */       if (c.getPlayer().isAdmin()) {
/*  69 */         c.getPlayer().dropMessage(5, "开始脚本任务 NPC：" + npcId + " Quest：" + questId);
       }
/*  71 */       ScriptEngine scriptengine = (ScriptEngine)iv;
/*  72 */       QuestActionManager qm = new QuestActionManager(c, npcId, questId, true, ScriptType.QUEST_START, iv);
/*  73 */       this.qms.put(c, qm);
/*  74 */       scriptengine.put("qm", qm);
/*  75 */       c.getPlayer().setConversation(1);
/*  76 */       c.setClickedNPC();
/*  77 */       iv.invokeFunction("start", new Object[] { Byte.valueOf((byte)1), Byte.valueOf((byte)0), Integer.valueOf(0) });
/*  78 */     } catch (Exception e) {
/*  79 */       log.error("执行任务脚本失败 任务ID: (" + questId + ")..NPCID: " + npcId + ".", e);
/*  80 */       dispose(c);
/*  81 */       notice(c, questId);
     }
   }

   public void startAction(MapleClient c, byte mode, byte type, int selection) {
/*  86 */     QuestActionManager qm = this.qms.get(c);
/*  87 */     if (qm == null) {
       return;
     }
     try {
/*  91 */       if (qm.pendingDisposal) {
/*  92 */         dispose(c);
       } else {
/*  94 */         c.setClickedNPC();
/*  95 */         qm.getIv().invokeFunction("start", new Object[] { Byte.valueOf(mode), Byte.valueOf(type), Integer.valueOf(selection) });
       }
/*  97 */     } catch (Exception e) {
/*  98 */       int npcId = qm.getNpc();
/*  99 */       int questId = qm.getQuest();
/* 100 */       log.error("执行任务脚本失败 任务ID: (" + questId + ")..NPCID: " + npcId + ".", e);
/* 101 */       dispose(c);
/* 102 */       notice(c, questId);
     }
   }

   public void endQuest(MapleClient c, int npcId, int questId, boolean customEnd) {
/* 107 */     if (c.getPlayer().isInJailMap()) {
/* 108 */       c.getPlayer().dropMessage(1, "在这个地方无法进行任务操作.");
       return;
     }
/* 111 */     if (!customEnd && !MapleQuest.getInstance(questId).canComplete(c.getPlayer(), null)) {
/* 112 */       if (c.getPlayer().isAdmin()) {
/* 113 */         c.getPlayer().dropMessage(6, "不能完成这个任务 NPC：" + npcId + " Quest：" + questId);
       }
       return;
     }
     try {
/* 118 */       if (!this.qms.containsKey(c) && c.canClickNPC()) {
/* 119 */         Invocable iv = getInvocable("quest/" + questId + ".js", c, true);
/* 120 */         if (iv == null) {
/* 121 */           if (c.getPlayer().isAdmin() && ServerConstants.isShowPacket()) {
/* 122 */             c.getPlayer().dropMessage(5, "完成任务脚本不存在 NPC：" + npcId + " Quest：" + questId);
           }

/* 125 */           if (!this.endQuestIds.contains(Integer.valueOf(questId))) {
/* 126 */             this.endQuestIds.add(Integer.valueOf(questId));
/* 127 */             MapleQuest.getInstance(questId).forceComplete(c.getPlayer(), npcId);
           }

           return;
         }
/* 132 */         if (c.getPlayer().isAdmin()) {
/* 133 */           c.getPlayer().dropMessage(5, "完成脚本任务 NPC：" + npcId + " Quest：" + questId);
         }
/* 135 */         ScriptEngine scriptengine = (ScriptEngine)iv;
/* 136 */         QuestActionManager qm = new QuestActionManager(c, npcId, questId, false, ScriptType.QUEST_END, iv);
/* 137 */         this.qms.put(c, qm);
/* 138 */         scriptengine.put("qm", qm);
/* 139 */         c.getPlayer().setConversation(1);
/* 140 */         c.setClickedNPC();
/* 141 */         iv.invokeFunction("end", new Object[] { Byte.valueOf((byte)1), Byte.valueOf((byte)0), Integer.valueOf(0) });
       }

     }
/* 145 */     catch (Exception e) {
/* 146 */       log.error("执行任务脚本失败 任务ID: (" + questId + ")..NPCID: " + npcId + ".", e);
/* 147 */       dispose(c);
/* 148 */       notice(c, questId);
     }
   }

   public void endAction(MapleClient c, byte mode, byte type, int selection) {
/* 153 */     QuestActionManager qm = this.qms.get(c);
/* 154 */     if (qm == null) {
       return;
     }
     try {
/* 158 */       if (qm.pendingDisposal) {
/* 159 */         dispose(c);
       } else {
/* 161 */         c.setClickedNPC();
/* 162 */         qm.getIv().invokeFunction("end", new Object[] { Byte.valueOf(mode), Byte.valueOf(type), Integer.valueOf(selection) });
       }
/* 164 */     } catch (Exception e) {
/* 165 */       int npcId = qm.getNpc();
/* 166 */       int questId = qm.getQuest();
/* 167 */       log.error("完成任务脚本失败 任务ID: (" + questId + ")..NPCID: " + npcId + ".", e);
/* 168 */       dispose(c);
/* 169 */       notice(c, questId);
     }
   }

   public void dispose(MapleClient c) {
/* 174 */     QuestActionManager qm = this.qms.get(c);
/* 175 */     if (qm != null) {
/* 176 */       this.qms.remove(c);
/* 177 */       c.removeScriptEngine(ServerConfig.WORLD_SCRIPTSPATH + "/quest/" + qm.getQuest() + ".js");
     }
/* 179 */     if (c.getPlayer() != null && c.getPlayer().getConversation() == 1) {
/* 180 */       c.getPlayer().setConversation(0);
     }
   }

   public void dispose(QuestActionManager qm, MapleClient c) {
/* 185 */     if (qm != null) {
/* 186 */       this.qms.remove(c);
/* 187 */       c.removeScriptEngine(ServerConfig.WORLD_SCRIPTSPATH + "/quest/" + qm.getQuest() + ".js");
     }
/* 189 */     if (c.getPlayer() != null && c.getPlayer().getConversation() == 1) {
/* 190 */       c.getPlayer().setConversation(0);
     }
   }

   public QuestActionManager getQM(MapleClient c) {
/* 195 */     return this.qms.get(c);
   }

   private void notice(MapleClient c, int questId) {
/* 199 */     c.getPlayer().dropMessage(1, "这个任务脚本是错误的，请联系管理员修复它.任务ID: " + questId);
   }
 }


/* Location:              C:\PGX\Server.jar!\scripting\quest\QuestScriptManager.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */